package com.zhuss.food.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.upload.DeleteUploadBo;
import com.zhuss.bean.bo.food.food.UpdateFoodBo;
import com.zhuss.bean.vo.food.food.DetailFoodVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.ProductType;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.FoodDao;
import com.zhuss.food.db.dao.ext.ProductDao;
import com.zhuss.food.db.entity.Food;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.ListBean;

/**
 * 修改餐品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("foodUpdate")
public class FoodUpdate extends AbstractProcessor<Object, UpdateFoodBo> implements Processor<Object, UpdateFoodBo> {

    @Autowired
    private FoodDao foodDao;
    
    @Autowired
    private ProductDao productDao;
    
    @Autowired
	@Qualifier("foodRead")
	private FoodRead foodRead;
    
    @Autowired
	@Qualifier("uploadDeleteProcessor")
	private Processor<Object, DeleteUploadBo> uploadDeleteProcessor;
    
    /**
     * 校验
     */
    @Override
    public void check(UpdateFoodBo bo) throws WpsException {
    	Long foodId = bo.getFoodId();
    	String name = bo.getName();
    	Integer area = bo.getArea();
    	
    	DetailFoodVo food = (DetailFoodVo) foodRead.detail(bo.getFoodId());
    	Integer foodArea = food.getArea();
    	
    	if (!foodArea.equals(area)) {
    		throw new WpsException(ExceptionCode.区域与餐品不匹配);
    	}
    	
    	if (foodId == null || foodId <= 0l) {
    		throw new WpsException(ExceptionCode.餐品ID为空);
    	}
    	if (name == null || "".equals(name.trim())) {
    		throw new WpsException(ExceptionCode.餐品name为空);
    	}
    	
    	List<Food> list = foodRead.listNotId(name, foodId, area);
    	if (!list.isEmpty()) {
    		throw new WpsException(ExceptionCode.餐品name已存在);
    	}
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(UpdateFoodBo param) throws WpsException {
    	
    	DetailFoodVo food = (DetailFoodVo) foodRead.detail(param.getFoodId());
    	String picDb = food.getPic();
    	
    	Food record = new Food();
    	record.setFoodId(param.getFoodId());
    	record.setCategory(param.getCategory());
    	record.setArea(param.getArea());
    	record.setName(param.getName());
    	record.setPrice(param.getPrice());
    	record.setIntroduction(param.getIntroduction());
    	record.setPic(param.getPic());
    	record.setSort(param.getSort());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	foodDao.updateByPrimaryKeySelective(record);
    	
    	Long foodId = record.getFoodId();
    	
    	productDao.newFresh(foodId, record.getCategory(), record.getName());
        productDao.newFreshPrice(foodId, record.getPrice(), new ListBean<Integer>().build(ProductType.一周菜单.key()).build(ProductType.点炒服务.key()).getList());
    	
    	String pic = param.getPic();
    	if (picDb != null && !"".equals(picDb.trim()) && !picDb.equals(pic)) {
    		DeleteUploadBo bo = new DeleteUploadBo();
    		BeanUtils.copyProperties(param, bo);
    		bo.setKey(picDb);
    		uploadDeleteProcessor.process(bo);
    	}
    	
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateFoodBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.餐品修改.key());
    	record.setTargetId(param.getFoodId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
